/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.images;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * <p>Project: jse2204 - TestWaterDemo
 * <p>Powered by webrx On 2022-09-26 14:38:31
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class TestWaterDemo {
    public static void main(String[] args) throws Exception {
        //加载图片
        String imgs = "c:/abc/g1.jpg";
        BufferedImage src = ImageIO.read(new File(imgs));
        int width = src.getWidth();
        int height = src.getHeight();

        Graphics2D g = src.createGraphics();

        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        String text = "版权所有 电话:123";
        int size = 55;

        Font f = Font.createFont(Font.TRUETYPE_FONT, new File("fonts/msyhl.ttc"));
        Font font = f.deriveFont(Font.BOLD, size);
        g.setFont(font);

        FontMetrics fm = g.getFontMetrics();
        int fw = fm.stringWidth(text);
        int fh = fm.getAscent() - (fm.getHeight() - fm.getAscent());
        g.setColor(new Color(0,0,0,88));

        //右上角
        int sp = 10;
        int x = sp;
        int y = fh + sp;

        //正中央
        x = (width - fw) / 2;
        y = (height - fh) / 2 + fh;
        g.drawString(text, x, y);
        g.dispose();

        imgs = "c:/abc/g1_txt.jpg";
        ImageIO.write(src, "jpg", new File(imgs));
        Runtime.getRuntime().exec("cmd /k start " + imgs);

    }
}
